#' 中介MR分析（两步法,第二步为多变量）
#'
#' @param EO_res 暴露到结局的res
#' @param EM_res 暴露到中介的res
#' @param mv_res 暴露和中介到结局的多变量的mv_res
#' @param method "1"：delta方法；"2"：误差传播法；"3"：另一篇文献的delta方法；
#'
#' @return 中介效应
#' @export
#'
#' @examples
#'
#' \dontrun{
#'
#' # 此功能只可用于单暴露，单中介，单结局
#' # INDIRECT = TOTAL (exposure -> mediator) x DIRECT (of mediator , mvmr)
#'
#' library(Oneclick)
#' exposure<- c('finn-b-HYPOTHYROIDISM')
#' mediation <- "F:/BaiduNetdiskDownload/1400种代谢物/GCST90200053.parquet"
#' outcome <- 'ebi-a-GCST90001390'
#'
#'
#'# 总效应 EO_res
#'exp_IVs<- U2_extract_instruments(exposure)
#'Outs<- U3_extract_outcomes_data(outcome = outcome ,exposure_iv = exp_IVs )
#'EO_dat<- U4_harmonise_data(exp_IVs,Outs)
#'EO_res<-U5_mr(EO_dat,run_mr_presso = FALSE,workers = 1)
#'
#'# 第一步，暴露到中介的单变量EM_res
#'Outs<- U3_extract_outcomes_data(outcome = mediation ,exposure_iv = exp_IVs )
#'EM_dat<- U4_harmonise_data(exp_IVs,Outs)
#'EM_res<-U5_mr(EM_dat,run_mr_presso = FALSE,workers = 1)
#'
#'
#'# 第二步，暴露和中介到结局的多变量分析结果中的中介到结局的效应，
#'mv_exposures <- M1_mv_extract_exposures(list(exposure,mediation))
#'mv_outcome <- M2_mv_extract_outcome( outcome = outcome,
#'                                     mv_exposures = mv_exposures)
#'mv_dat<-M3_mv_harmonise_data( mv_exposures = mv_exposures,
#'                              mv_outcome = mv_outcome )
#'
#'
#'# TwoSampleMR给出的IVW MVMR 的结果
#'mv_res<- TwoSampleMR::mv_multiple(mv_dat)[["result"]] %>%
#'  TwoSampleMR::generate_odds_ratios()
#'
#'mediation_effect <- M8_two_step2(EO_res,EM_res,mv_res)
#'
#'
#'
#' }
#'
#'
#'
#'
#'
M8_two_step2 <- function(EO_res,EM_res,mv_res,method="1"){

  #暴露到结局
  EO_b <- ifelse(!is.na(EO_res$`b_Inverse variance weighted`),
                 EO_res$`b_Inverse variance weighted`,
                 EO_res$`b_Wald ratio`)
  EO_se <- ifelse(!is.na(EO_res$`se_Inverse variance weighted`),
                  EO_res$`se_Inverse variance weighted`,
                  EO_res$`se_Wald ratio`)



  #暴露到中介
  EM_b <- ifelse(!is.na(EM_res$`b_Inverse variance weighted`),
                 EM_res$`b_Inverse variance weighted`,
                 EM_res$`b_Wald ratio`)
  EM_se <- ifelse(!is.na(EM_res$`se_Inverse variance weighted`),
                  EM_res$`se_Inverse variance weighted`,
                  EM_res$`se_Wald ratio`)


  #中介到结局
  exposure_name<-EO_res$exposure[1]

  MO_b <- (subset( mv_res, !exposure==exposure_name ))$b
  MO_se <- (subset( mv_res, !exposure==exposure_name ))$se

  # 中介效应，也就是间接效应
  if(method=="1" ){
  mediation_effect <- mediation_prop(EM_beta=EM_b,EM_se=EM_se,
                                     MO_beta=MO_b, MO_se= MO_se,
                                     EO_beta=EO_b, EO_se= EO_se)}


  if(method=="2" ){
    mediation_effect <- product_method_PoE(EM_beta=EM_b,EM_se=EM_se,
                                        MO_beta=MO_b, MO_se= MO_se ) }

  if(method=="3" ){
    mediation_effect <- product_method_Delta(EM_beta=EM_b,EM_se=EM_se,
                                          MO_beta=MO_b, MO_se= MO_se ) }

  mediation_effect$id.exposure <- EO_res$id.exposure
  mediation_effect$id.mediation <- ( subset( mv_res, !exposure==exposure_name ) )$id.exposure
  mediation_effect$id.outcome <- EO_res$id.outcome
  mediation_effect$exposure <- EO_res$exposure
  mediation_effect$mediation <- ( subset( mv_res, !exposure==exposure_name ) )$exposure
  mediation_effect$outcome <- EO_res$outcome

  mediation_effect <- mediation_effect %>% dplyr::select(id.exposure, id.mediation,id.outcome,
                             exposure, mediation,outcome,everything())

  return( mediation_effect )
}
